package com.longrise.android.byjk.widget.recyclerviewadapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by sjz on 15/4/25.
 */
// TODO 待优化,notifyItemInserted,notifyItemRangeRemoved,notifyItemRemoved
public abstract class ArrayAdapter<T> extends HeaderFooterAdapter {

    private List<T> mDataList;

    public ArrayAdapter() {
        this(new ArrayList<T>());
    }

    public ArrayAdapter(List<T> dataList) {
        this.mDataList = dataList;
    }

    public void add(T item) {
        mDataList.add(item);
        reportNotifyItemInserted(mDataList.size() - 1);
    }

    public void add(int index, T item) {
        mDataList.add(index, item);
        notifyDataSetChanged();
    }

    public void addAll(Collection<? extends T> collection) {
        for (T item : collection) {
            mDataList.add(item);
            reportNotifyItemInserted(mDataList.size() - 1);
        }
    }

    public void replace(T object, int position) {
        mDataList.set(position, object);
        reportNotifyItemChanged(position);
    }

    public void clear() {
        mDataList.clear();
        notifyDataSetChanged();
    }

    public void remove(T object) {
        int position = indexOf(object);
        mDataList.remove(position);
        reportNotifyItemRemoved(position);
    }

    public void remove(int index) {
        mDataList.remove(index);
        notifyDataSetChanged();
    }

    public int indexOf(T object) {
        return mDataList.indexOf(object);
    }

    public T getItem(int position) {
        return mDataList.get(position);
    }

    public List<T> getDataList() {
        return mDataList;
    }

    @Override
    protected int getContentItemCount() {
        return mDataList.size();
    }
}
